#include "Camera.h"

Camera::Camera()
{
}

void	Camera::setShape( float angle, float aspectRatio, float nearN, float farN )
{
		gluPerspective( angle, aspectRatio, nearN, farN );
}


void	Camera::lookAt( float eyex, float eyey, float eyez,
				float lookx, float looky, float lookz,
				float upx, float upy, float upz )
{
	eye = Point3(eyex,eyey,eyez);
	up=Vector3(upx,upy,upz);
	n=Vector3(eyex-lookx,eyey-looky,eyez-lookz);

	up.cross(n,u); 
	n.cross(u,v);

	n.normalize();
	v.normalize();
	u.normalize();
}

void	Camera::lookAt( Point3 Eye, Point3 Look, Vector3 Up )
{

}

void	Camera::setModelViewMatrix()
{
	Vector3 eyet=Vector3(eye.x,eye.y,eye.z);
	float   M[4][4];

	M[0][0]=u.x;		   M[0][1]=v.x;			  M[0][2]=n.x;		       M[0][3]=0;
	M[1][0]=u.y;		   M[1][1]=v.y;			  M[1][2]=n.y;			   M[1][3]=0;
	M[2][0]=u.z;		   M[2][1]=v.z;			  M[2][2]=n.z;			   M[2][3]=0;
	M[3][0]=-eyet.dot(u);   M[3][1]=-eyet.dot(v);   M[3][2]=-eyet.dot(n);     M[3][3]=1; 

	glMatrixMode(GL_MODELVIEW);
	glLoadMatrixf((float*)M);
}

void	Camera::roll( float angle )
{
	float csinv=(float)(cos(PI/180*angle));
	float sinv=(float)(sin(PI/180*angle));
	// Snurrar runt n-axeln
	//
	// u'= cos(vinkel)u + sin(vinkel)v
	// v'=-sin(vinkel)u + cos(vinkel)v
	u.set(csinv*u.x+sinv*v.x,csinv*u.y+sinv*v.y,csinv*u.z+sinv*v.z);
	v.set(-sinv*u.x+csinv*v.x,-sinv*u.y+csinv*v.y,-sinv*u.z+csinv*v.z);
}

void	Camera::pitch( float angle )
{
	float csinv=(float)(cos(PI/180*angle));
	float sinv=(float)(sin(PI/180*angle));

	// Snurrar runt u-axeln
	//
	// n'= cos(vinkel)v + sin(vinkel)n
	// v'=-sin(vinkel)v + cos(vinkel)n

	v.set(csinv*v.x+sinv*n.x, csinv*v.y+sinv*n.y,csinv*v.z+sinv*n.z);
	n.set(-sinv*v.x+csinv*n.x,-sinv*v.y+csinv*n.y,-sinv*v.z+csinv*n.z);
}

void	Camera::yaw( float angle )
{
	float csinv=(float)(cos(PI/180*angle));
	float sinv=(float)(sin(PI/180*angle));

	// Snurrar runt v-axeln
	//
	// n'= cos(vinkel)u + sin(vinkel)n
	// u'=-sin(vinkel)u + cos(vinkel)n

	u.set(csinv*u.x+sinv*n.x, csinv*u.y+sinv*n.y,csinv*u.z+sinv*n.z);
	n.set(-sinv*u.x+csinv*n.x,-sinv*u.y+csinv*n.y,-sinv*u.z+csinv*n.z);

}

void	Camera::slide( float Upart, float Vpart, float Npart )
{
	eye.x += Upart*u.x + Vpart*v.x + Npart*n.x;
	eye.y += Upart*u.y + Vpart*v.y + Npart*n.y;
	eye.z += Upart*u.z + Vpart*v.z + Npart*n.z;
}

void    Camera::setEye(float x,float y, float z) {

	eye.x=x;
	eye.y=y;
	eye.z=z;
}